import tkinter.filedialog
import os
import pathlib
import re


if __name__ == "__main__":
    
    
    
    # 打开对话框选择文件
    # select mainfiles
    mainfiles = tkinter.filedialog.askopenfilenames(
        initialdir=os.getcwd(),
        title="选择主图文件",
        filetypes=[("All Image Files", "*.jpg;*.png;*.gif;*.webp")]
    )
    
    # sort mainfiles
    mainfiles = list(mainfiles)
    mainfiles.sort(key=lambda item: int(re.search(r"\d+", pathlib.Path(item).resolve().stem).group()))

    # rename mainfiles
    for index, file in enumerate(iterable=mainfiles, start=1):
        current_index = index
        mainfilelocaltion_directory = pathlib.Path(file).resolve().parent
        mainfilename = pathlib.Path(file).name
        filebasename = pathlib.Path(file).stem
        filesuffixname = pathlib.Path(file).suffix

        new_filesuffixname = filesuffixname
        new_filebasename = f"Main-{index:03}"
        new_filebasename = new_filebasename.replace("主", "-").replace("图", "-").replace("首", "-")

        new_mainfilename = f"{new_filebasename}{new_filesuffixname}"
        print(os.path.join(mainfilelocaltion_directory,mainfilename), '\t', os.path.join(mainfilelocaltion_directory, new_mainfilename))
        os.rename(os.path.join(mainfilelocaltion_directory,mainfilename), os.path.join(mainfilelocaltion_directory, new_mainfilename))
        

    # select detailfiles
    detailfiles = tkinter.filedialog.askopenfilenames(
        initialdir=os.getcwd(),
        title="选择详情图文件",
        filetypes=[("All Image Files", "*.jpg;*.png;*.gif;*.webp")]
    )

    # sort detailfiles
    detailfiles = list(detailfiles)
    detailfiles.sort(key=lambda item: int(re.search(r"\d+", pathlib.Path(item).resolve().stem).group()))

    # rename detailfiles
    for index, file in enumerate(iterable=detailfiles, start=1):
        current_index = index
        detailfilelocaltion_directory = pathlib.Path(file).resolve().parent
        detailfilename = pathlib.Path(file).name
        filebasename = pathlib.Path(file).stem
        filesuffixname = pathlib.Path(file).suffix

        new_filesuffixname = filesuffixname
        new_filebasename = f"Detail-{index:03}"
        new_filebasename = new_filebasename.replace("详情", "-").replace("长图", "-")
        new_detailfilename = f"{new_filebasename}{new_filesuffixname}"

        print(detailfilename, '\t', new_detailfilename)
        print(os.path.join(detailfilelocaltion_directory,detailfilename), '\t', os.path.join(detailfilelocaltion_directory, new_detailfilename))
        os.rename(os.path.join(detailfilelocaltion_directory,detailfilename), os.path.join(detailfilelocaltion_directory, new_detailfilename))

    # select skufiles
    skufiles = tkinter.filedialog.askopenfilenames(
        initialdir=os.getcwd(),
        title="选择 SKU 图文件",
        filetypes=[("All Image Files", "*.jpg;*.png;*.gif;*.webp")]
    )

    # sort skufiles
    skufiles = list(skufiles)
    skufiles.sort(key=lambda item: int(re.search(r"\d+", pathlib.Path(item).resolve().stem).group()))
    for index, file in enumerate(iterable=skufiles, start=1):
        current_index = index
        skufilelocaltion_directory = pathlib.Path(file).resolve().parent
        skufilename = pathlib.Path(file).name
        filebasename = pathlib.Path(file).stem
        filesuffixname = pathlib.Path(file).suffix
        new_filesuffixname = filesuffixname
        new_filebasename = f"SKU-{index:03}"
        new_filebasename = new_filebasename.replace("规格", "-").replace("属性", "-")
        new_skufilename = f"{new_filebasename}{new_filesuffixname}"
        print(skufilename, '\t', new_skufilename)

        
        print(os.path.join(skufilelocaltion_directory,skufilename), '\t', os.path.join(skufilelocaltion_directory, new_skufilename))
        os.rename(os.path.join(skufilelocaltion_directory,skufilename), os.path.join(skufilelocaltion_directory, new_skufilename))

    